import { ProfileAction } from './../../types/store.d'
import { User, UserProfile } from './../../types/data.d'

type ProfileUser = {
  user: User
  edit: UserProfile
}
const initialState: ProfileUser = {
  //登录人的信息
  user: {},
  edit: {},
} as ProfileUser
// export const profile = (
//   state = initialState,
//   action: ProfileAction
// ): ProfileUser => {
//   if (action.type === 'getUser/profile') {
//     return {
//       ...state,
//       user: action.payload,
//     }
//   }
//   return state
// }
export const profile = (state = initialState, action: ProfileAction) => {
  //  存储登录人信息
  if (action.type === 'getUser/profile') {
    return {
      ...state,
      user: action.payload,
    }
  }
  if (action.type === 'getEditUser/profile') {
    return {
      ...state,
      edit: action.payload,
    }
  }

  // 更新修改用户信息
  if (action.type === 'update/profile') {
    return {
      ...state,
      // 新状态
      edit: {
        ...state.edit,
        // 替换之前修改的信息
        ...action.payload,
      },
    }
  }
  return state
}
